package com.wise.blockingque.delayqueue;

import java.util.concurrent.DelayQueue;

public class Consumer implements Runnable {
    /**
     * 延时队列，消费者从其中获取消息进行消费
     */
    private DelayQueue<Message> queue;

    public Consumer(DelayQueue<Message> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        while (true) {
            try {
                //如果获取元素时，如果元素还没有到达指定时间，则将当前线程阻塞此处，直致能够获取
                Message take = queue.take();
                System.out.println("消费消息id:" + take.getId() + " 消息体:" + take.getBody());
                if (queue.isEmpty()) break;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
